<?php

use common\helpers\Url;
use yii\widgets\ActiveForm;
use kartik\datetime\DateTimePicker;
use common\helpers\ArrayHelper;
use common\enums\StatusEnum;
use common\enums\MassRecordSendTypeEnum;

$this->title = $model->isNewRecord ? '创建' : '编辑';
$this->params['breadcrumbs'][] = ['label' => '发送邮件', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>

<?php $form = ActiveForm::begin([
    'id' => 'massRecord',
    'enableAjaxValidation' => true,
    'validationUrl' => Url::to(['edit', 'id' => $model['id']]),
]); ?>
<div class="row">
    <div class="col-lg-12">
        <div class="box">
            <div class="box-header with-border">
                <h3 class="box-title">基本信息</h3>
            </div>
            <div class="box-body">
                <div class="col-lg-12">
                    <?= $form->field($model, 'tag_id')->dropDownList(ArrayHelper::merge([0 => '全部会员'], $tags)) ?>
                    <?= $form->field($model, 'send_type')->radioList(MassRecordSendTypeEnum::getMap(), [
                            'class' => 'sendType'
                    ]) ?>
                    <div id="sendTime" class="<?php if ($model->send_type != MassRecordSendTypeEnum::TIMING) { echo 'hide'; }; ?>">
                        <?= $form->field($model, 'send_time', [
                            'options' => [
                                'class' => $model->send_time == 1 ? 'hide' : '',
                                'id' => 'send_time'
                            ]
                        ])->widget(DateTimePicker::class, [
                            'language' => 'zh-CN',
                            'options' => [
                                'value' => $model->isNewRecord ? date('Y-m-d H:i', strtotime(date('Y-m-d H:i'))) : date('Y-m-d H:i', $model->send_time),
                            ],
                            'pluginOptions' => [
                                'format' => 'yyyy-mm-dd hh:ii',
                                'todayHighlight' => true,//今日高亮
                                'autoclose' => true,//选择后自动关闭
                                'todayBtn' => true,//今日按钮显示
                            ]
                        ])->hint('注意：需要开启定时任务');?>
                    </div>
                    <div id="testAccount" class="<?php if ($model->send_type != MassRecordSendTypeEnum::TEST) { echo 'hide'; }; ?>">
                        <?= $form->field($model, 'test_account')->textInput(); ?>
                    </div>
                    <?= $form->field($model, 'title')->textInput(); ?>
                    <?= $form->field($model, 'data')->widget(\common\widgets\ueditor\UEditor::class) ?>
                </div>
            </div>
            <div class="box-footer text-center">
                <?php if($model->isNewRecord || $model->send_type == MassRecordSendTypeEnum::TEST) {?>
                    <button class="btn btn-primary" type="submit">保存</button>
                <?php } ?>
                <span class="btn btn-white" onclick="history.go(-1)">返回</span>
            </div>
        </div>
    </div>
</div>
<?php ActiveForm::end(); ?>

<script>
    $("input[name='MassRecord[send_type]']").click(function () {
        var val = $(this).val();
        $('#sendTime').addClass('hide');
        $('#testAccount').addClass('hide');

        if (parseInt(val) === 2) {
            $('#sendTime').removeClass('hide');
        }

        if (parseInt(val) === 3) {
            $('#testAccount').removeClass('hide');
        }
    });
</script>
